import Request from "./request.js"
// import {router,RouterMount} from '@/common/router.js'
import apiList from "./api.js"
import store from "@/store/index.js"

export default function api (url, data = {}) {
    const request = new Request();
    console.log(request,'request');
    let api = getApiObj(url);
    // 请求之前拦截器
    request.interceptor.request((config, cancel) => {
        console.log(api,'api');
        
    	return config
    });
    // 请求之后拦截器
    request.interceptor.response((response) => {
		console.log(response,'response')
        // console.log(response,'response');
        
        // if (response.status === 0 || response.status === -1) {
        //     uni.showToast({
        //     	title: response.msg || '请求出错,稍后重试',
        //     	icon: 'none',
        //     	duration: 1000,
        //     	mask: true
        //     });
        // }
        // console.log(response.status,'common/request/index');
        // 服务端返回的状态码不等于200,则  reject()
        // if (response.status === 401) {      
            // 是否需要删除token 或者其他东西
        // }
        return response
    }, (response) => {
        return response
    })
    return request.request({
        url:api.url,
        data,
        method: api.method
    })
}

function getApiObj(url) {
    let apiArray = url.split(".");
    let api = apiList;
    apiArray.forEach(v => {
        api = api[v]
    })
    return api;
}